﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portfolio.BL.Interfaces;
using Portfolio.Entities;
using Portfolio.Repository.Common;
using Portfolio.Repository.Articles;
using Portfolio.Repository.Interfaces;


namespace Portfolio.BL.Articles
{
    public class ArticlesBL : IArticlesBL
    {
        private Portfolio3Entities _context = new Portfolio3Entities();

        private IList<ArtilcesGridV> _listArticles;       

        private IArticlesRepository _articlesRepository;

        public ArticlesBL() { }        

        public void UpdateArticle(ArticleT Article)
        {
            _articlesRepository = new ArticlesRepository(_context);
            _articlesRepository.UpdateArticle(Article);
        }

        public int InsertArticle(ArticleT Article)
        {
            _articlesRepository = new ArticlesRepository(_context);
            return _articlesRepository.InsertArticle(Article);
        }

        public ArtilcesGridV ReturnArticleById(int idArticle)
        {
            _articlesRepository = new ArticlesRepository(_context);
            return _articlesRepository.GetArticleByIdArticle(idArticle);
        }

        public IList<ArtilcesGridV> SortBy(IQueryable<ArtilcesGridV> source, string sortExpression, string sortDirection) 
        {           
            switch (sortExpression) 
            {   
                case "Subtitle":
                    if (sortDirection == "Descending") 
                    {
                        return source.OrderByDescending(p => p.SubTitle).ToList();
                    }
                    source = source.OrderBy(p => p.SubTitle);
                break;

                case "Title":
                    if (sortDirection == "Descending")
                    {
                        return source.OrderByDescending(p => p.Title).ToList();
                    }
                    source = source.OrderBy(p => p.Title);
                break;

                case "NameCategory":
                    if (sortDirection == "Descending")
                    {
                        return source.OrderByDescending(p => p.NameCategory).ToList();
                    }
                    source = source.OrderBy(p => p.NameCategory);
                break;

                case "SeqArticleCategory":
                    if (sortDirection == "Descending")
                    {
                        return source.OrderByDescending(p => p.SeqArticleCategory).ToList();
                    }
                    source = source.OrderBy(p => p.SeqArticleCategory);
                break;
                
                case "Status":
                    if (sortDirection == "Descending")
                    {
                        return source.OrderByDescending(p => p.Status).ToList();
                    }
                    source = source.OrderBy(p => p.Status);
                break;
                
                case "ActiveFlag":
                    if (sortDirection == "Descending")
                    {
                        return source.OrderByDescending(p => p.ActiveFlag).ToList();
                    }
                    source = source.OrderBy(p => p.ActiveFlag);
                break;

                case "ModifiedDate":
                    if (sortDirection == "Descending")
                    {
                        return source.OrderByDescending(p => p.ModifiedDate).ToList();
                    }
                    source = source.OrderBy(p => p.ModifiedDate);
                break;        
                        


            }
            return source.ToList();

        }

        public void DeleteArticle(ArticleT Article)
        {
            _articlesRepository = new ArticlesRepository(_context);         

            _articlesRepository.DeleteArticle(Article);
        }


        public IList<ArtilcesGridV> ReturnArticleBySearch(string title, int? idCategory, int? status, bool? active, string sortExpression, string sortDirection)
        {
            _articlesRepository = new ArticlesRepository(_context);
            _listArticles = _articlesRepository.GetArticleBySearch(title, idCategory, status, active);           
            
            return SortBy(_listArticles.AsQueryable(), sortExpression, sortDirection);
        }

        public IList<ArticleT> ReturnArticleByIDIDArticle(int? idCategory)
        {
            // _articlesRepository = new ArticlesRepository(_context);
            //_listArticles = _articlesRepository.GetArticleBySearch(title, idCategory, status, active);           
            
            //return
            return null;
        }


        public IDictionary<int, string> ReturnStatusEnum()
        {
            return null;//Enumeration.GetAll<StatusEnum>();
        }

        public IDictionary<int, string> ReturnActiveEnum()
        {
            return null;//Enumeration.GetAll<ActiveEnum>();
        }

        public IDictionary<int, string> ReturnPageSizeEnum()
        {
            return null;//Enumeration.GetAll<PageSizeEnum>();
        }
    }
}
